<?php

class EcommerceController extends MemberController {

    public function actionUpgrade() {

        $action = function_lib::uri_segment(4);
        switch ($action) {
            case "empty":
                unset($_SESSION['upgrade']['cart']);
                $_SESSION['upgrade'] = array();
                $this->redirect('list');
                break;

            case "checkout":
                $this->actionUpgradeCheckout();
                break;

            default:
                $this->actionUpgradeTransaction();
                break;
        }
    }

    public function actionAuto() {
        $action = function_lib::uri_segment(4);

        switch ($action) {
            case "empty":
                unset($_SESSION['auto']['cart']);
                $_SESSION['auto'] = array();
                $this->redirect('list');
                break;

            case "checkout":
                $this->actionAutoCheckout();
                break;

            default:
                $this->actionAutoTransaction();
                break;
        }
    }

    public function actionVoucher() {
        $action = function_lib::uri_segment(4);

        switch ($action) {
            case "empty":
                unset($_SESSION['voucher']['cart']);
                $_SESSION['voucher'] = array();
                $this->redirect('list');
                break;

            case "checkout":
                $this->actionVoucherCheckout();
                break;

            default:
                $this->actionVoucherTransaction();
                break;
        }
    }

    public function actionExtra() {
        $action = function_lib::uri_segment(4);

        switch ($action) {
            case "empty":
                unset($_SESSION['extra']['cart']);
                $_SESSION['extra'] = array();
                $this->redirect('list');
                break;

            case "checkout":
                $this->actionExtraCheckout();
                break;

            default:
                $this->actionExtraTransaction();
                break;
        }
    }

    public function actionUpgradeTransaction() {

        //jika belum ada session untuk produk upgrade, maka buat session produk upgrade
        if (!isset($_SESSION['upgrade'])) {
            $_SESSION['upgrade'] = array();
        }

        //input produk dari list produk ke dalam keranjang belanja
        if (isset($_POST['addcart'])) {
            if (isset($_POST['add'])) {
                $products_id = $_POST['add'];
                if (!isset($_SESSION['upgrade']['cart'][$products_id])) {
                    $_SESSION['upgrade']['cart'][$products_id] = 0;
                }
                $_SESSION['upgrade']['cart'][$products_id] += 1;
            }
        }

        //jika checkout
        if (isset($_POST['checkout'])) {
            $this->redirect('checkout');
        }

        //update quantity produk
        if (isset($_POST['updatecart'])) {
            if (is_array($_POST['quantity'])) {
                $is_success = true;
                foreach ($_POST['quantity'] as $products_id => $quantity) {
                    if ($quantity == 0) {
                        unset($_SESSION['upgrade']['cart'][$products_id]);
                    } else {
                        //cari harga per produk
                        $products_price = function_lib::get_one("products_price", "ecom_products", "products_id = '" . $products_id . "'");

                        //hitung tambahan jumlah produk 
                        $add_quantity = $quantity - $_POST['old_quantity'][$products_id];

                        //cek apakah saldo cukup untuk penambahan jumlah produk, jika tidak maka jumlah produk tidak ada penambahan
                        if ($_SESSION['ewallet_cash']['saldo_available'] >= ($products_price * $add_quantity)) {
                            $_SESSION['ewallet_cash']['saldo_available'] -= ( $products_price * $add_quantity);
                            $_SESSION['upgrade']['cart'][$products_id] = $quantity;
                        } else {
                            $is_success = false;
                            $_SESSION['upgrade']['cart'][$products_id] = $_POST['old_quantity'][$products_id];
                        }
                    }
                }
                if ($is_success == false) {
                    Yii::app()->user->setFlash('error', 'Proses penambahan item gagal diproses. Saldo E-Wallet Utama anda tidak mencukupi.');
                }
            }
        }

        //hapus item produk dari keranjang belanja
        if (isset($_POST['delete'])) {
            if (is_array($_POST['delete'])) {
                foreach ($_POST['delete'] as $products_id => $value) {
                    unset($_SESSION['upgrade']['cart'][$products_id]);
                }
                Yii::app()->user->setFlash('success', 'Hapus item dari Keranjang Belanja Ugrade berhasil.');
            }
        }

        //data produk yang ada dalam keranjang belanja
        $cart = '';
        $_SESSION['ewallet_cash']['total_cart'] = 0;
        $total_quantity = $total_price = 0;
        if (isset($_SESSION['upgrade']['cart'])) {
            foreach ($_SESSION['upgrade']['cart'] as $products_id => $quantity) {
                $cart_products = vo_ecommerce::get_products_by_id($products_id);
                $cart[$products_id] = $cart_products;
                $cart[$products_id]['quantity'] = $quantity;
                $cart[$products_id]['total'] = $cart_products['products_price'] * $quantity;
                $total_quantity += $quantity;
                $total_price += $cart[$products_id]['total'];
            }
            //simpan total harga keranjang belanja upgrade ke dalam session
            $_SESSION['ewallet_cash']['total_cart'] = $total_price;
        }
        $_SESSION['ewallet_cash']['saldo_active'] = function_lib::get_one("(ewallet_cash_acc - ewallet_cash_paid)", "sys_ewallet", "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'");
        $_SESSION['ewallet_cash']['max_shipping_cost'] = ($_SESSION['ewallet_cash']['saldo_active'] > Yii::app()->params['config']['max_shipping_cost']) ? Yii::app()->params['config']['max_shipping_cost'] : $_SESSION['ewallet_cash']['saldo_active'];
        $_SESSION['ewallet_cash']['saldo_available'] = $_SESSION['ewallet_cash']['saldo_active'] - ($_SESSION['ewallet_cash']['max_shipping_cost'] + $_SESSION['ewallet_cash']['total_cart']);

        //data produk yang tersedia
        $products = vo_ecommerce::get_products_by_category_type('UP');

        $this->render('upgrade_transaction', array(
            'products' => $products,
            'cart' => $cart,
            'total_quantity' => $total_quantity,
            'total_price' => $total_price,
        ));
    }

    public function actionUpgradeCheckout() {
        $model = new captcha_form;

        if (!isset($_SESSION['upgrade']['cart'])) {
            Yii::app()->controller->redirect(array('/member/ecommerce/upgrade/list'));
        }

        if (isset($_POST['submit'])) {
            $model->attributes = $_POST['captcha_form'];

            if ($_POST['recipient_name'] == '' || $_POST['recipient_address'] == '' || $_POST['recipient_phone'] == '') {
                Yii::app()->user->setFlash('error', 'Data Penerima yang anda masukkan belum lengkap. Silakan melengkapi data penerima terlebih dahulu.');
                $this->redirect('checkout');
            } else {
                $pin_validation = vo_network::pin_validation(Yii::app()->user->getState('network_id'), $_POST['pin']);
                if (!$pin_validation) {
                    Yii::app()->user->setFlash('error', 'Maaf, PIN yang anda masukkan belum benar.');
                    Yii::app()->controller->redirect(array('/member/ecommerce/upgrade/checkout'));
                } elseif (!$model->validate()) {
                    Yii::app()->user->setFlash('error', 'Maaf, Kode Unik yang anda masukkan belum benar.');
                    Yii::app()->controller->redirect(array('/member/ecommerce/upgrade/checkout'));
                } else {
                    //cek kembali saldo ewallet langsung dari database sebelum transaksi dijalankan
                    $saldo_active = function_lib::get_one("(ewallet_cash_acc - ewallet_cash_paid)", "sys_ewallet", "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'");
                    $max_shipping_cost = Yii::app()->params['config']['max_shipping_cost'];
                    $saldo_available = $saldo_active - $max_shipping_cost;

                    if ($saldo_available < $_POST['total_price']) {
                        Yii::app()->user->setFlash('error', 'Transaksi gagal diproses. Saldo anda tidak mencukupi.');
                        $this->redirect('checkout');
                    } else {
                        $transaction = Yii::app()->db->beginTransaction();
                        $datetime = date("Y-m-d H:i:s");
                        try {
                            //masukkan ke tabel transaksi
                            $transaction_code = date("ymd") . str_pad(vo_ecommerce::get_num_transaction_today(), 5, "0", STR_PAD_LEFT);

                            $rs_transaction = Yii::app()->db->createCommand()->insert('ecom_transaction', array(
                                'transaction_code' => $transaction_code,
                                'transaction_buyer_network_id' => Yii::app()->user->getState('network_id'),
                                'transaction_recipient_name' => $_POST['recipient_name'],
                                'transaction_recipient_address' => $_POST['recipient_address'],
                                'transaction_recipient_phone' => $_POST['recipient_phone'],
                                'transaction_total_quantity' => $_POST['total_quantity'],
                                'transaction_total_price' => $_POST['total_price'],
                                'transaction_datetime' => $datetime,
                                'transaction_is_approve' => '0',
                                    ));

                            //masukkan ke tabel detail transaksi
                            $transaction_id = Yii::app()->db->getLastInsertId();
                            $arr_order = $_POST['order_upgrade'];
                            foreach ($arr_order as $id => $products_id) {

                                $rs_transaction_detail = Yii::app()->db->createCommand()->insert('ecom_transaction_detail', array(
                                    'transaction_detail_transaction_id' => $transaction_id,
                                    'transaction_detail_products_id' => $products_id,
                                    'transaction_detail_products_code' => $_POST['products_code'][$products_id],
                                    'transaction_detail_products_name' => $_POST['products_name'][$products_id],
                                    'transaction_detail_quantity' => $_POST['products_quantity'][$products_id],
                                    'transaction_detail_price' => $_POST['products_price'][$products_id],
                                        ));

                                //cari serial sesuai kategori produk dan sejumlah jumlah produk
                                for ($i = 1; $i <= $_POST['products_quantity'][$products_id]; $i++) {
                                    //cari serial produk yang available
                                    $serial_products = vo_ecommerce::get_serial_products($_POST['products_products_category_id'][$products_id]);

                                    //jika serial tersedia, maka update serial produk
                                    //jika tidak, maka masukkan ke dalam serial request
                                    if ($serial_products != false) {
                                        //update serial produk
                                        $sql_serial_products_update = "UPDATE sys_serial_products 
                                SET serial_products_is_active = '1', 
                                serial_products_is_buyed = '1', 
                                serial_products_active_datetime = '" . $datetime . "' 
                                WHERE serial_products_id = '" . $serial_products['serial_products_id'] . "'";
                                        Yii::app()->db->createCommand($sql_serial_products_update)->execute();

                                        //masukkan ke dalam serial buyer
                                        Yii::app()->db->createCommand()->insert('sys_serial_products_buyer', array(
                                            'serial_products_buyer_serial_products_id' => $serial_products['serial_products_id'],
                                            'serial_products_buyer_network_id' => Yii::app()->user->getState('network_id'),
                                            'serial_products_buyer_datetime' => $datetime,
                                            'serial_products_buyer_products_id' => $products_id,
                                        ));
                                    } else {
                                        //masukkan ke dalam serial request
                                        Yii::app()->db->createCommand()->insert('sys_serial_products_request', array(
                                            'serial_products_request_network_id' => Yii::app()->user->getState('network_id'),
                                            'serial_products_request_products_category_id' => $_POST['products_products_category_id'][$products_id],
                                            'serial_products_request_datetime' => $datetime,
                                            'serial_products_request_is_accepted' => '0',
                                            'serial_products_request_products_id' => $products_id,
                                        ));
                                    }
                                }
                            }
                            //kurangi saldo e-wallet cash
                            $sql_ewallet_update = "UPDATE sys_ewallet 
                        SET ewallet_cash_paid = ewallet_cash_paid + " . $_POST['total_price'] . " 
                        WHERE ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'";
                            Yii::app()->db->createCommand($sql_ewallet_update)->execute();

                            //masukkan log e-wallet
                            Yii::app()->db->createCommand()->insert('sys_ewallet_log', array(
                                'ewallet_log_network_id' => Yii::app()->user->getState('network_id'),
                                'ewallet_log_type' => 'cash',
                                'ewallet_log_value' => $_POST['total_price'],
                                'ewallet_log_status' => 'debit',
                                'ewallet_log_datetime' => $datetime,
                                'ewallet_log_note' => 'Belanja Produk Upgrade (Kode Transaksi: ' . $transaction_code . ')',
                            ));

                            $transaction->commit();

                            unset($_SESSION['upgrade']['cart']);
                            $_SESSION['upgrade'] = array();

                            Yii::app()->user->setFlash('success', 'Transaksi anda berhasil diproses');
                        } catch (CDbException $e) {
                            $transaction->rollback();
                            Yii::app()->user->setFlash('error', 'Transaksi anda gagal diproses. Silakan ulangi proses Transaksi.');
                        }
                        $this->redirect('list');
                    }
                }
            }
        }

        //data produk yang ada dalam keranjang belanja
        $cart = '';
        $total_quantity = $total_price = 0;
        if (isset($_SESSION['upgrade']['cart'])) {
            foreach ($_SESSION['upgrade']['cart'] as $products_id => $quantity) {
                $cart_products = vo_ecommerce::get_products_by_id($products_id);
                $cart[$products_id] = $cart_products;
                $cart[$products_id]['quantity'] = $quantity;
                $cart[$products_id]['total'] = $cart_products['products_price'] * $quantity;
                $total_quantity += $quantity;
                $total_price += $cart[$products_id]['total'];
            }
        }

        //data pengirim / member
        $member = sys_member::model()->findByPk(Yii::app()->user->getState('network_id'));

        $this->render('upgrade_checkout', array(
            'member' => $member,
            'cart' => $cart,
            'total_quantity' => $total_quantity,
            'total_price' => $total_price,
            'model' => $model,
        ));
    }

    public function actionAutoTransaction() {
        //jika belum ada session untuk produk tutup poin, maka buat session produk tutup poin
        if (!isset($_SESSION['auto'])) {
            $_SESSION['auto'] = array();
        }

        //input produk dari list produk ke dalam keranjang belanja
        if (isset($_POST['addcart'])) {
            if (isset($_POST['add'])) {
                $products_id = $_POST['add'];
                if (!isset($_SESSION['auto']['cart'][$products_id])) {
                    $_SESSION['auto']['cart'][$products_id] = 0;
                }
                $_SESSION['auto']['cart'][$products_id] += 1;
            }
        }

        //jika checkout
        if (isset($_POST['checkout'])) {
            $this->redirect('checkout');
        }

        //update quantity produk
        if (isset($_POST['updatecart'])) {
            if (is_array($_POST['quantity'])) {
                $is_success = true;
                foreach ($_POST['quantity'] as $products_id => $quantity) {
                    if ($quantity == 0) {
                        unset($_SESSION['auto']['cart'][$products_id]);
                    } else {
                        //cari harga per produk
                        $products_price = function_lib::get_one("products_price", "ecom_products", "products_id = '" . $products_id . "'");

                        //hitung tambahan jumlah produk 
                        $add_quantity = $quantity - $_POST['old_quantity'][$products_id];

                        //cek apakah saldo cukup untuk penambahan jumlah produk, jika tidak maka jumlah produk tidak ada penambahan
                        if ($_SESSION['ewallet_products']['saldo_available'] >= ($products_price * $add_quantity)) {
                            $_SESSION['ewallet_products']['saldo_available'] -= ( $products_price * $add_quantity);
                            $_SESSION['auto']['cart'][$products_id] = $quantity;
                        } else {
                            $is_success = false;
                            $_SESSION['auto']['cart'][$products_id] = $_POST['old_quantity'][$products_id];
                        }
                    }
                }
                if ($is_success == false) {
                    Yii::app()->user->setFlash('error', 'Proses penambahan item gagal diproses. Saldo E-Wallet Produk anda tidak mencukupi.');
                }
            }
        }

        //hapus item produk dari keranjang belanja
        if (isset($_POST['delete'])) {
            if (is_array($_POST['delete'])) {
                foreach ($_POST['delete'] as $products_id => $value) {
                    unset($_SESSION['auto']['cart'][$products_id]);
                }
                Yii::app()->user->setFlash('success', 'Hapus item dari Keranjang Belanja Bulanan berhasil.');
            }
        }

        //data produk yang ada dalam keranjang belanja
        $cart = '';
        $_SESSION['ewallet_products']['total_cart'] = 0;
        $total_quantity = $total_price = 0;
        if (isset($_SESSION['auto']['cart'])) {
            foreach ($_SESSION['auto']['cart'] as $products_id => $quantity) {
                $cart_products = vo_ecommerce::get_products_by_id($products_id);
                $cart[$products_id] = $cart_products;
                $cart[$products_id]['quantity'] = $quantity;
                $cart[$products_id]['total'] = $cart_products['products_price'] * $quantity;
                $total_quantity += $quantity;
                $total_price += $cart[$products_id]['total'];
            }
            //simpan total harga keranjang belanja tutup poin ke dalam session
            $_SESSION['ewallet_products']['total_cart'] = $total_price;
        }
        $_SESSION['ewallet_products']['saldo_active'] = function_lib::get_one("(ewallet_products_acc - ewallet_products_paid)", "sys_ewallet", "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'");
        $_SESSION['ewallet_products']['max_shipping_cost'] = ($_SESSION['ewallet_products']['saldo_active'] > Yii::app()->params['config']['max_shipping_cost']) ? Yii::app()->params['config']['max_shipping_cost'] : $_SESSION['ewallet_products']['saldo_active'];
        $_SESSION['ewallet_products']['saldo_available'] = $_SESSION['ewallet_products']['saldo_active'] - ($_SESSION['ewallet_products']['max_shipping_cost'] + $_SESSION['ewallet_products']['total_cart']);

        //data produk yang tersedia
        $products = vo_ecommerce::get_products_by_category_type('AUTO');

        $this->render('auto_transaction', array(
            'products' => $products,
            'cart' => $cart,
            'total_quantity' => $total_quantity,
            'total_price' => $total_price,
        ));
    }

    public function actionAutoCheckout() {
        $model = new captcha_form;

        if (!isset($_SESSION['auto']['cart'])) {
            Yii::app()->controller->redirect(array('/member/ecommerce/auto/list'));
        }

        if (isset($_POST['submit'])) {
            $model->attributes = $_POST['captcha_form'];

            if ($_POST['recipient_name'] == '' || $_POST['recipient_address'] == '' || $_POST['recipient_phone'] == '') {
                Yii::app()->user->setFlash('error', 'Data Penerima yang anda masukkan belum lengkap. Silakan melengkapi data penerima terlebih dahulu.');
                $this->redirect('checkout');
            } else {
                $pin_validation = vo_network::pin_validation(Yii::app()->user->getState('network_id'), $_POST['pin']);
                if (!$pin_validation) {
                    Yii::app()->user->setFlash('error', 'Maaf, PIN yang anda masukkan belum benar.');
                    Yii::app()->controller->redirect(array('/member/ecommerce/auto/checkout'));
                } elseif (!$model->validate()) {
                    Yii::app()->user->setFlash('error', 'Maaf, Kode Unik yang anda masukkan belum benar.');
                    Yii::app()->controller->redirect(array('/member/ecommerce/auto/checkout'));
                } else {
                    //cek kembali saldo ewallet langsung dari database sebelum transaksi dijalankan
                    $saldo_active = function_lib::get_one("(ewallet_products_acc - ewallet_products_paid)", "sys_ewallet", "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'");
                    $max_shipping_cost = Yii::app()->params['config']['max_shipping_cost'];
                    $saldo_available = $saldo_active - $max_shipping_cost;

                    if ($saldo_available < $_POST['total_price']) {
                        Yii::app()->user->setFlash('error', 'Transaksi gagal diproses. Saldo anda tidak mencukupi.');
                        $this->redirect('checkout');
                    } else {
                        $transaction = Yii::app()->db->beginTransaction();
                        $datetime = date("Y-m-d H:i:s");
                        try {
                            //masukkan ke tabel transaksi
                            $transaction_code = date("ymd") . str_pad(vo_ecommerce::get_num_transaction_today(), 5, "0", STR_PAD_LEFT);

                            $rs_transaction = Yii::app()->db->createCommand()->insert('ecom_transaction', array(
                                'transaction_code' => $transaction_code,
                                'transaction_buyer_network_id' => Yii::app()->user->getState('network_id'),
                                'transaction_recipient_name' => $_POST['recipient_name'],
                                'transaction_recipient_address' => $_POST['recipient_address'],
                                'transaction_recipient_phone' => $_POST['recipient_phone'],
                                'transaction_total_quantity' => $_POST['total_quantity'],
                                'transaction_total_price' => $_POST['total_price'],
                                'transaction_datetime' => $datetime,
                                'transaction_is_approve' => '0',
                                'transaction_is_approve' => '5',
                                    ));

                            //masukkan ke tabel detail transaksi
                            $transaction_id = Yii::app()->db->getLastInsertId();
                            $arr_order = $_POST['order_auto'];
                            foreach ($arr_order as $id => $products_id) {
                                $products_bv = function_lib::get_one("products_bv", "ecom_products", "products_id = '" . $products_id . "'");
                                $rs_transaction_detail = Yii::app()->db->createCommand()->insert('ecom_transaction_detail', array(
                                    'transaction_detail_transaction_id' => $transaction_id,
                                    'transaction_detail_products_id' => $products_id,
                                    'transaction_detail_products_code' => $_POST['products_code'][$products_id],
                                    'transaction_detail_products_name' => $_POST['products_name'][$products_id],
                                    'transaction_detail_quantity' => $_POST['products_quantity'][$products_id],
                                    'transaction_detail_price' => $_POST['products_price'][$products_id],
                                    'transaction_detail_bv' => $products_bv,
                                        ));

                                //cari serial sesuai kategori produk dan sejumlah jumlah produk
                                for ($i = 1; $i <= $_POST['products_quantity'][$products_id]; $i++) {
                                    //cari serial produk yang available
                                    $serial_products = vo_ecommerce::get_serial_products($_POST['products_products_category_id'][$products_id]);

                                    //jika serial tersedia, maka update serial produk
                                    //jika tidak, maka masukkan ke dalam serial request
                                    if ($serial_products != false) {
                                        //update serial produk
                                        $sql_serial_products_update = "UPDATE sys_serial_products 
                                SET serial_products_is_active = '1', 
                                serial_products_is_buyed = '1', 
                                serial_products_active_datetime = '" . $datetime . "' 
                                WHERE serial_products_id = '" . $serial_products['serial_products_id'] . "'";
                                        Yii::app()->db->createCommand($sql_serial_products_update)->execute();

                                        //masukkan ke dalam serial buyer
                                        Yii::app()->db->createCommand()->insert('sys_serial_products_buyer', array(
                                            'serial_products_buyer_serial_products_id' => $serial_products['serial_products_id'],
                                            'serial_products_buyer_network_id' => Yii::app()->user->getState('network_id'),
                                            'serial_products_buyer_datetime' => $datetime,
                                        ));
                                    } else {
                                        //masukkan ke dalam serial request
                                        Yii::app()->db->createCommand()->insert('sys_serial_products_request', array(
                                            'serial_products_request_network_id' => Yii::app()->user->getState('network_id'),
                                            'serial_products_request_products_category_id' => $_POST['products_products_category_id'][$products_id],
                                            'serial_products_request_datetime' => $datetime,
                                            'serial_products_request_is_accepted' => '0',
                                        ));
                                    }
                                }
                            }
                            //kurangi saldo e-wallet products
                            $sql_ewallet_update = "UPDATE sys_ewallet 
                        SET ewallet_products_paid = ewallet_products_paid + " . $_POST['total_price'] . " 
                        WHERE ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'";
                            Yii::app()->db->createCommand($sql_ewallet_update)->execute();

                            //masukkan log e-wallet
                            Yii::app()->db->createCommand()->insert('sys_ewallet_log', array(
                                'ewallet_log_network_id' => Yii::app()->user->getState('network_id'),
                                'ewallet_log_type' => 'products',
                                'ewallet_log_value' => $_POST['total_price'],
                                'ewallet_log_status' => 'debit',
                                'ewallet_log_datetime' => $datetime,
                                'ewallet_log_note' => 'Belanja Produk Belanja Bulanan (Kode Transaksi: ' . $transaction_code . ')',
                            ));

                            $transaction->commit();

                            unset($_SESSION['auto']['cart']);
                            $_SESSION['auto'] = array();

                            Yii::app()->user->setFlash('success', 'Transaksi anda berhasil diproses');
                        } catch (CDbException $e) {
                            $transaction->rollback();
                            Yii::app()->user->setFlash('error', 'Transaksi anda gagal diproses. Silakan ulangi proses Transaksi.');
                        }
                        $this->redirect('list');
                    }
                }
            }
        }

        //data produk yang ada dalam keranjang belanja
        $cart = '';
        $total_quantity = $total_price = 0;
        if (isset($_SESSION['auto']['cart'])) {
            foreach ($_SESSION['auto']['cart'] as $products_id => $quantity) {
                $cart_products = vo_ecommerce::get_products_by_id($products_id);
                $cart[$products_id] = $cart_products;
                $cart[$products_id]['quantity'] = $quantity;
                $cart[$products_id]['total'] = $cart_products['products_price'] * $quantity;
                $total_quantity += $quantity;
                $total_price += $cart[$products_id]['total'];
            }
        }

        //data pengirim / member
        $member = sys_member::model()->findByPk(Yii::app()->user->getState('network_id'));

        $this->render('auto_checkout', array(
            'member' => $member,
            'cart' => $cart,
            'total_quantity' => $total_quantity,
            'total_price' => $total_price,
            'model' => $model,
        ));
    }

    public function actionVoucherTransaction() {
        //jika belum ada session untuk produk tutup poin, maka buat session produk tutup poin
        if (!isset($_SESSION['voucher'])) {
            $_SESSION['voucher'] = array();
        }

        //input produk dari list produk ke dalam keranjang belanja
        if (isset($_POST['addcart'])) {
            if (isset($_POST['add'])) {
                $products_id = $_POST['add'];
                if (!isset($_SESSION['voucher']['cart'][$products_id])) {
                    $_SESSION['voucher']['cart'][$products_id] = 0;
                }
                $_SESSION['voucher']['cart'][$products_id] += 1;
            }
        }

        //jika checkout
        if (isset($_POST['checkout'])) {
            $this->redirect('checkout');
        }

        //update quantity produk
        if (isset($_POST['updatecart'])) {
            if (is_array($_POST['quantity'])) {
                $is_success = true;
                foreach ($_POST['quantity'] as $products_id => $quantity) {
                    if ($quantity == 0) {
                        unset($_SESSION['voucher']['cart'][$products_id]);
                    } else {
                        //cari harga per produk
                        $products_price = function_lib::get_one("products_price", "ecom_products", "products_id = '" . $products_id . "'");

                        //hitung tambahan jumlah produk 
                        $add_quantity = $quantity - $_POST['old_quantity'][$products_id];

                        //cek apakah saldo cukup untuk penambahan jumlah produk, jika tidak maka jumlah produk tidak ada penambahan
                        if ($_SESSION['ewallet_voucher']['saldo_available'] >= ($products_price * $add_quantity)) {
                            $_SESSION['ewallet_voucher']['saldo_available'] -= ( $products_price * $add_quantity);
                            $_SESSION['voucher']['cart'][$products_id] = $quantity;
                        } else {
                            $is_success = false;
                            $_SESSION['voucher']['cart'][$products_id] = $_POST['old_quantity'][$products_id];
                        }
                    }
                }
                if ($is_success == false) {
                    Yii::app()->user->setFlash('error', 'Proses penambahan item gagal diproses. Saldo E-Wallet Voucher anda tidak mencukupi.');
                }
            }
        }

        //hapus item produk dari keranjang belanja
        if (isset($_POST['delete'])) {
            if (is_array($_POST['delete'])) {
                foreach ($_POST['delete'] as $products_id => $value) {
                    unset($_SESSION['voucher']['cart'][$products_id]);
                }
                Yii::app()->user->setFlash('success', 'Hapus item dari Keranjang Belanja Bulanan berhasil.');
            }
        }

        //data produk yang ada dalam keranjang belanja
        $cart = '';
        $_SESSION['ewallet_voucher']['total_cart'] = 0;
        $total_quantity = $total_price = 0;
        if (isset($_SESSION['voucher']['cart'])) {
            foreach ($_SESSION['voucher']['cart'] as $products_id => $quantity) {
                $cart_products = vo_ecommerce::get_products_by_id($products_id);
                $cart[$products_id] = $cart_products;
                $cart[$products_id]['quantity'] = $quantity;
                $cart[$products_id]['total'] = $cart_products['products_price'] * $quantity;
                $total_quantity += $quantity;
                $total_price += $cart[$products_id]['total'];
            }
            //simpan total harga keranjang belanja tutup poin ke dalam session
            $_SESSION['ewallet_voucher']['total_cart'] = $total_price;
        }
        $_SESSION['ewallet_voucher']['saldo_active'] = function_lib::get_one("(ewallet_voucher_acc - ewallet_voucher_paid)", "sys_ewallet", "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'");
        $_SESSION['ewallet_voucher']['max_shipping_cost'] = ($_SESSION['ewallet_voucher']['saldo_active'] > Yii::app()->params['config']['max_shipping_cost']) ? Yii::app()->params['config']['max_shipping_cost'] : $_SESSION['ewallet_voucher']['saldo_active'];
        $_SESSION['ewallet_voucher']['saldo_available'] = $_SESSION['ewallet_voucher']['saldo_active'] - ($_SESSION['ewallet_voucher']['max_shipping_cost'] + $_SESSION['ewallet_voucher']['total_cart']);

        //data produk yang tersedia
        $products = vo_ecommerce::get_products_by_category_type('VOUCHER');

        $this->render('voucher_transaction', array(
            'products' => $products,
            'cart' => $cart,
            'total_quantity' => $total_quantity,
            'total_price' => $total_price,
        ));
    }

    public function actionVoucherCheckout() {
        $model = new captcha_form;

        if (!isset($_SESSION['voucher']['cart'])) {
            Yii::app()->controller->redirect(array('/member/ecommerce/voucher/list'));
        }

        if (isset($_POST['submit'])) {
            $model->attributes = $_POST['captcha_form'];

            if ($_POST['recipient_name'] == '' || $_POST['recipient_address'] == '' || $_POST['recipient_phone'] == '') {
                Yii::app()->user->setFlash('error', 'Data Penerima yang anda masukkan belum lengkap. Silakan melengkapi data penerima terlebih dahulu.');
                $this->redirect('checkout');
            } else {
                $pin_validation = vo_network::pin_validation(Yii::app()->user->getState('network_id'), $_POST['pin']);
                if (!$pin_validation) {
                    Yii::app()->user->setFlash('error', 'Maaf, PIN yang anda masukkan belum benar.');
                    Yii::app()->controller->redirect(array('/member/ecommerce/voucher/checkout'));
                } elseif (!$model->validate()) {
                    Yii::app()->user->setFlash('error', 'Maaf, Kode Unik yang anda masukkan belum benar.');
                    Yii::app()->controller->redirect(array('/member/ecommerce/voucher/checkout'));
                } else {
                    //cek kembali saldo ewallet langsung dari database sebelum transaksi dijalankan
                    $saldo_active = function_lib::get_one("(ewallet_voucher_acc - ewallet_voucher_paid)", "sys_ewallet", "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'");
                    $max_shipping_cost = Yii::app()->params['config']['max_shipping_cost'];
                    $saldo_available = $saldo_active - $max_shipping_cost;

                    if ($saldo_available < $_POST['total_price']) {
                        Yii::app()->user->setFlash('error', 'Transaksi gagal diproses. Saldo anda tidak mencukupi.');
                        $this->redirect('checkout');
                    } else {
                        $transaction = Yii::app()->db->beginTransaction();
                        $datetime = date("Y-m-d H:i:s");
                        try {
                            //masukkan ke tabel transaksi
                            $transaction_code = date("ymd") . str_pad(vo_ecommerce::get_num_transaction_today(), 5, "0", STR_PAD_LEFT);

                            $rs_transaction = Yii::app()->db->createCommand()->insert('ecom_transaction', array(
                                'transaction_code' => $transaction_code,
                                'transaction_buyer_network_id' => Yii::app()->user->getState('network_id'),
                                'transaction_recipient_name' => $_POST['recipient_name'],
                                'transaction_recipient_address' => $_POST['recipient_address'],
                                'transaction_recipient_phone' => $_POST['recipient_phone'],
                                'transaction_total_quantity' => $_POST['total_quantity'],
                                'transaction_total_price' => $_POST['total_price'],
                                'transaction_datetime' => $datetime,
                                'transaction_is_approve' => '0',
                                'transaction_is_approve' => '10',
                                    ));

                            //masukkan ke tabel detail transaksi
                            $transaction_id = Yii::app()->db->getLastInsertId();
                            $arr_order = $_POST['order_voucher'];
                            foreach ($arr_order as $id => $products_id) {
                                $products_bv = function_lib::get_one("products_bv", "ecom_products", "products_id = '" . $products_id . "'");
                                $rs_transaction_detail = Yii::app()->db->createCommand()->insert('ecom_transaction_detail', array(
                                    'transaction_detail_transaction_id' => $transaction_id,
                                    'transaction_detail_products_id' => $products_id,
                                    'transaction_detail_products_code' => $_POST['products_code'][$products_id],
                                    'transaction_detail_products_name' => $_POST['products_name'][$products_id],
                                    'transaction_detail_quantity' => $_POST['products_quantity'][$products_id],
                                    'transaction_detail_price' => $_POST['products_price'][$products_id],
                                    'transaction_detail_bv' => $products_bv,
                                        ));

                                //cari serial sesuai kategori produk dan sejumlah jumlah produk
                                for ($i = 1; $i <= $_POST['products_quantity'][$products_id]; $i++) {
                                    //cari serial produk yang available
                                    $serial_products = vo_ecommerce::get_serial_products($_POST['products_products_category_id'][$products_id]);

                                    //jika serial tersedia, maka update serial produk
                                    //jika tidak, maka masukkan ke dalam serial request
                                    if ($serial_products != false) {
                                        //update serial produk
                                        $sql_serial_products_update = "UPDATE sys_serial_products 
                                SET serial_products_is_active = '1', 
                                serial_products_is_buyed = '1', 
                                serial_products_active_datetime = '" . $datetime . "' 
                                WHERE serial_products_id = '" . $serial_products['serial_products_id'] . "'";
                                        Yii::app()->db->createCommand($sql_serial_products_update)->execute();

                                        //masukkan ke dalam serial buyer
                                        Yii::app()->db->createCommand()->insert('sys_serial_products_buyer', array(
                                            'serial_products_buyer_serial_products_id' => $serial_products['serial_products_id'],
                                            'serial_products_buyer_network_id' => Yii::app()->user->getState('network_id'),
                                            'serial_products_buyer_datetime' => $datetime,
                                        ));
                                    } else {
                                        //masukkan ke dalam serial request
                                        Yii::app()->db->createCommand()->insert('sys_serial_products_request', array(
                                            'serial_products_request_network_id' => Yii::app()->user->getState('network_id'),
                                            'serial_products_request_products_category_id' => $_POST['products_products_category_id'][$products_id],
                                            'serial_products_request_datetime' => $datetime,
                                            'serial_products_request_is_accepted' => '0',
                                        ));
                                    }
                                }
                            }
                            //kurangi saldo e-wallet products
                            $sql_ewallet_update = "UPDATE sys_ewallet 
                        SET ewallet_voucher_paid = ewallet_voucher_paid + " . $_POST['total_price'] . " 
                        WHERE ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'";
                            Yii::app()->db->createCommand($sql_ewallet_update)->execute();

                            //masukkan log e-wallet
                            Yii::app()->db->createCommand()->insert('sys_ewallet_log', array(
                                'ewallet_log_network_id' => Yii::app()->user->getState('network_id'),
                                'ewallet_log_type' => 'voucher',
                                'ewallet_log_value' => $_POST['total_price'],
                                'ewallet_log_status' => 'debit',
                                'ewallet_log_datetime' => $datetime,
                                'ewallet_log_note' => 'Belanja Produk Belanja Voucher (Kode Transaksi: ' . $transaction_code . ')',
                            ));

                            $transaction->commit();

                            unset($_SESSION['voucher']['cart']);
                            $_SESSION['voucher'] = array();

                            Yii::app()->user->setFlash('success', 'Transaksi anda berhasil diproses');
                        } catch (CDbException $e) {
                            $transaction->rollback();
                            Yii::app()->user->setFlash('error', 'Transaksi anda gagal diproses. Silakan ulangi proses Transaksi.');
                        }
                        $this->redirect('list');
                    }
                }
            }
        }

        //data produk yang ada dalam keranjang belanja
        $cart = '';
        $total_quantity = $total_price = 0;
        if (isset($_SESSION['voucher']['cart'])) {
            foreach ($_SESSION['voucher']['cart'] as $products_id => $quantity) {
                $cart_products = vo_ecommerce::get_products_by_id($products_id);
                $cart[$products_id] = $cart_products;
                $cart[$products_id]['quantity'] = $quantity;
                $cart[$products_id]['total'] = $cart_products['products_price'] * $quantity;
                $total_quantity += $quantity;
                $total_price += $cart[$products_id]['total'];
            }
        }

        //data pengirim / member
        $member = sys_member::model()->findByPk(Yii::app()->user->getState('network_id'));

        $this->render('voucher_checkout', array(
            'member' => $member,
            'cart' => $cart,
            'total_quantity' => $total_quantity,
            'total_price' => $total_price,
            'model' => $model,
        ));
    }

    public function actionLog() {

        $action = function_lib::uri_segment(4);

        if ($action == 'detail') {
            $this->actionLogDetail();
        } else {
            $data = vo_ecommerce::get_transaction(Yii::app()->user->getState('network_id'));

            $this->render('log', array(
                'data' => $data,
            ));
        }
    }

    public function actionLogDetail() {

        $transaction_code = function_lib::uri_segment(5);

        if ($transaction_code != '') {
            $data = vo_ecommerce::get_transaction_by_id(Yii::app()->user->getState('network_id'), $transaction_code);

            if ($data == false) {
                Yii::app()->user->setFlash('error', 'Maaf, transaksi anda dengan Kode Transaksi ' . $transaction_code . ' tidak ditemukan.');
                Yii::app()->controller->redirect(array('/member/ecommerce/log'));
            } else {
                $data['status'] = ($data['transaction_is_approve'] == '1') ? 'Diproses' : 'Pending';
                $detail = vo_ecommerce::get_transaction_detail($data['transaction_id']);

                $this->render('log_detail', array(
                    'data' => $data,
                    'detail' => $detail,
                ));
            }
        } else {
            Yii::app()->controller->redirect(array('/member/ecommerce/log'));
        }
    }

    public function actionSerial() {
        $model = new captcha_form;

        if (isset($_POST['captcha_form'])) {
            $model->attributes = $_POST['captcha_form'];

            $pin_validation = vo_network::pin_validation(Yii::app()->user->getState('network_id'), $_POST['pin']);
            if (!$pin_validation) {
                Yii::app()->user->setFlash('error', 'Maaf, PIN yang anda masukkan belum benar.');
                Yii::app()->controller->redirect(array('/member/ecommerce/serial'));
            } elseif (!$model->validate()) {
                Yii::app()->user->setFlash('error', 'Maaf, Kode Unik yang anda masukkan belum benar.');
                Yii::app()->controller->redirect(array('/member/ecommerce/serial'));
            } else {
                $serial_used = vo_ecommerce::get_serial_products_buyer(Yii::app()->user->getState('network_id'), '1');
                $serial_unused = vo_ecommerce::get_serial_products_buyer(Yii::app()->user->getState('network_id'), '0');
                $serial_request = vo_ecommerce::get_serial_products_request(Yii::app()->user->getState('network_id'));

                $this->render('serial', array(
                    'serial_used' => $serial_used,
                    'serial_unused' => $serial_unused,
                    'serial_request' => $serial_request,
                ));
            }
        } else {
            $this->render('pin_validation', array(
                'model' => $model,
            ));
        }
    }

    /*
     * Untuk ecommerce Extra
     */

    public function actionExtraTransaction() {
        //jika belum ada session untuk produk tutup poin, maka buat session produk tutup poin
        if (!isset($_SESSION['extra'])) {
            $_SESSION['extra'] = array();
        }

        //input produk dari list produk ke dalam keranjang belanja
        if (isset($_POST['addcart'])) {
            if (isset($_POST['add'])) {
                $products_id = $_POST['add'];
                if (!isset($_SESSION['extra']['cart'][$products_id])) {
                    $_SESSION['extra']['cart'][$products_id] = 0;
                }
                $_SESSION['extra']['cart'][$products_id] += 1;
            }
        }

        //jika checkout
        if (isset($_POST['checkout'])) {
            $this->redirect('checkout');
        }

        //update quantity produk
        if (isset($_POST['updatecart'])) {
            if (is_array($_POST['quantity'])) {
                $is_success = true;
                foreach ($_POST['quantity'] as $products_id => $quantity) {
                    if ($quantity == 0) {
                        unset($_SESSION['extra']['cart'][$products_id]);
                    } else {
                        //cari harga per produk
                        $products_price = function_lib::get_one("products_price", "ecom_products", "products_id = '" . $products_id . "'");

                        //hitung tambahan jumlah produk 
                        $add_quantity = $quantity - $_POST['old_quantity'][$products_id];

                        //cek apakah saldo cukup untuk penambahan jumlah produk, jika tidak maka jumlah produk tidak ada penambahan
                        if ($_SESSION['ewallet_extra']['saldo_available'] >= ($products_price * $add_quantity)) {
                            $_SESSION['ewallet_extra']['saldo_available'] -= ($products_price * $add_quantity);
                            $_SESSION['extra']['cart'][$products_id] = $quantity;
                        } else {
                            $is_success = false;
                            $_SESSION['extra']['cart'][$products_id] = $_POST['old_quantity'][$products_id];
                        }
                    }
                }
                if ($is_success == false) {
                    Yii::app()->user->setFlash('error', 'Proses penambahan item gagal diproses. Saldo E-Wallet Extra anda tidak mencukupi.');
                }
            }
        }

        //hapus item produk dari keranjang belanja
        if (isset($_POST['delete'])) {
            if (is_array($_POST['delete'])) {
                foreach ($_POST['delete'] as $products_id => $value) {
                    unset($_SESSION['extra']['cart'][$products_id]);
                }
                Yii::app()->user->setFlash('success', 'Hapus item dari Keranjang Belanja Bulanan berhasil.');
            }
        }

        //data produk yang ada dalam keranjang belanja
        $cart = '';
        $_SESSION['ewallet_extra']['total_cart'] = 0;
        $total_quantity = $total_price = 0;
        if (isset($_SESSION['extra']['cart'])) {
            foreach ($_SESSION['extra']['cart'] as $products_id => $quantity) {
                $cart_products = vo_ecommerce::get_products_by_id($products_id);
                $cart[$products_id] = $cart_products;
                $cart[$products_id]['quantity'] = $quantity;
                $cart[$products_id]['total'] = $cart_products['products_price'] * $quantity;
                $total_quantity += $quantity;
                $total_price += $cart[$products_id]['total'];
            }
            //simpan total harga keranjang belanja tutup poin ke dalam session
            $_SESSION['ewallet_extra']['total_cart'] = $total_price;
        }
        $_SESSION['ewallet_extra']['saldo_active'] = function_lib::get_one("(ewallet_extra_acc - ewallet_extra_paid)", "sys_ewallet", "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'");
        $_SESSION['ewallet_extra']['max_shipping_cost'] = ($_SESSION['ewallet_extra']['saldo_active'] > Yii::app()->params['config']['max_shipping_cost']) ? Yii::app()->params['config']['max_shipping_cost'] : $_SESSION['ewallet_extra']['saldo_active'];
        $_SESSION['ewallet_extra']['saldo_available'] = $_SESSION['ewallet_extra']['saldo_active'] - ($_SESSION['ewallet_extra']['max_shipping_cost'] + $_SESSION['ewallet_extra']['total_cart']);

        //data produk yang tersedia
        $products = vo_ecommerce::get_products_by_category_type('extra');

        $this->render('extra_transaction', array(
            'products' => $products,
            'cart' => $cart,
            'total_quantity' => $total_quantity,
            'total_price' => $total_price,
        ));
    }

    public function actionExtraCheckout() {
        $model = new captcha_form;

        if (!isset($_SESSION['extra']['cart'])) {
            Yii::app()->controller->redirect(array('/member/ecommerce/extra/list'));
        }

        if (isset($_POST['submit'])) {
            $model->attributes = $_POST['captcha_form'];

            if ($_POST['recipient_name'] == '' || $_POST['recipient_address'] == '' || $_POST['recipient_phone'] == '') {
                Yii::app()->user->setFlash('error', 'Data Penerima yang anda masukkan belum lengkap. Silakan melengkapi data penerima terlebih dahulu.');
                $this->redirect('checkout');
            } else {
                $pin_validation = vo_network::pin_validation(Yii::app()->user->getState('network_id'), $_POST['pin']);
                if (!$pin_validation) {
                    Yii::app()->user->setFlash('error', 'Maaf, PIN yang anda masukkan belum benar.');
                    Yii::app()->controller->redirect(array('/member/ecommerce/extra/checkout'));
                } elseif (!$model->validate()) {
                    Yii::app()->user->setFlash('error', 'Maaf, Kode Unik yang anda masukkan belum benar.');
                    Yii::app()->controller->redirect(array('/member/ecommerce/extra/checkout'));
                } else {
                    //cek kembali saldo ewallet langsung dari database sebelum transaksi dijalankan
                    $saldo_active = function_lib::get_one("(ewallet_extra_acc - ewallet_extra_paid)", "sys_ewallet", "ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'");
                    $max_shipping_cost = Yii::app()->params['config']['max_shipping_cost'];
                    $saldo_available = $saldo_active - $max_shipping_cost;

                    if ($saldo_available < $_POST['total_price']) {
                        Yii::app()->user->setFlash('error', 'Transaksi gagal diproses. Saldo anda tidak mencukupi.');
                        $this->redirect('checkout');
                    } else {
                        $transaction = Yii::app()->db->beginTransaction();
                        $datetime = date("Y-m-d H:i:s");
                        try {
                            //masukkan ke tabel transaksi
                            $transaction_code = date("ymd") . str_pad(vo_ecommerce::get_num_transaction_today(), 5, "0", STR_PAD_LEFT);

                            $rs_transaction = Yii::app()->db->createCommand()->insert('ecom_transaction', array(
                                'transaction_code' => $transaction_code,
                                'transaction_buyer_network_id' => Yii::app()->user->getState('network_id'),
                                'transaction_recipient_name' => $_POST['recipient_name'],
                                'transaction_recipient_address' => $_POST['recipient_address'],
                                'transaction_recipient_phone' => $_POST['recipient_phone'],
                                'transaction_total_quantity' => $_POST['total_quantity'],
                                'transaction_total_price' => $_POST['total_price'],
                                'transaction_datetime' => $datetime,
                                'transaction_is_approve' => '0',
                                'transaction_is_approve' => '11',
                                    ));

                            //masukkan ke tabel detail transaksi
                            $transaction_id = Yii::app()->db->getLastInsertId();
                            $arr_order = $_POST['order_extra'];
                            //print_r($arr_order);
                            foreach ($arr_order as $id => $products_id) {

                                /*
                                 * $products_bv = function_lib::get_one("products_bv", "ecom_products", "products_id = '" . $products_id . "'");
                                  $rs_transaction_detail = Yii::app()->db->createCommand()->insert('ecom_transaction_detail', array(
                                  'transaction_detail_transaction_id' => $transaction_id,
                                  'transaction_detail_products_id' => $products_id,
                                  'transaction_detail_products_code' => $_POST['products_code'][$products_id],
                                  'transaction_detail_products_name' => $_POST['products_name'][$products_id],
                                  'transaction_detail_quantity' => $_POST['products_quantity'][$products_id],
                                  'transaction_detail_price' => $_POST['products_price'][$products_id],
                                  'transaction_detail_bv' => $products_bv,
                                  ));
                                 */
                                $products_bv = function_lib::get_one("products_bv", "ecom_products", "products_id = '" . $products_id . "'");
                                $rs_transaction_detail = Yii::app()->db->createCommand()->insert('ecom_transaction_detail', array(
                                    'transaction_detail_transaction_id' => $transaction_id,
                                    'transaction_detail_products_id' => $products_id,
                                    'transaction_detail_products_code' => $_POST['products_code'][$products_id],
                                    'transaction_detail_products_name' => $_POST['products_name'][$products_id],
                                    'transaction_detail_quantity' => $_POST['products_quantity'][$products_id],
                                    'transaction_detail_price' => $_POST['products_price'][$products_id],
                                    'transaction_detail_bv' => $products_bv,
                                        ));

                                //cari serial sesuai kategori produk dan sejumlah jumlah produk
                                for ($i = 1; $i <= $_POST['products_quantity'][$products_id]; $i++) {
                                    //cari serial produk yang available
                                    $serial_products = vo_ecommerce::get_serial_products($_POST['products_products_category_id'][$products_id]);

                                    //jika serial tersedia, maka update serial produk
                                    //jika tidak, maka masukkan ke dalam serial request
                                    if ($serial_products != false) {
                                        //update serial produk
                                        $sql_serial_products_update = "UPDATE sys_serial_products 
                                SET serial_products_is_active = '1', 
                                serial_products_is_buyed = '1', 
                                serial_products_active_datetime = '" . $datetime . "' 
                                WHERE serial_products_id = '" . $serial_products['serial_products_id'] . "'";
                                        Yii::app()->db->createCommand($sql_serial_products_update)->execute();

                                        //masukkan ke dalam serial buyer
                                        Yii::app()->db->createCommand()->insert('sys_serial_products_buyer', array(
                                            'serial_products_buyer_serial_products_id' => $serial_products['serial_products_id'],
                                            'serial_products_buyer_network_id' => Yii::app()->user->getState('network_id'),
                                            'serial_products_buyer_datetime' => $datetime,
                                        ));
                                    } else {
                                        //masukkan ke dalam serial request
                                        Yii::app()->db->createCommand()->insert('sys_serial_products_request', array(
                                            'serial_products_request_network_id' => Yii::app()->user->getState('network_id'),
                                            'serial_products_request_products_category_id' => $_POST['products_products_category_id'][$products_id],
                                            'serial_products_request_datetime' => $datetime,
                                            'serial_products_request_is_accepted' => '0',
                                        ));
                                    }
                                }
                            }
                            //kurangi saldo e-wallet products
                            $sql_ewallet_update = "UPDATE sys_ewallet 
                        SET ewallet_extra_paid = ewallet_extra_paid + " . $_POST['total_price'] . " 
                        WHERE ewallet_network_id = '" . Yii::app()->user->getState('network_id') . "'";
                            Yii::app()->db->createCommand($sql_ewallet_update)->execute();

                            //masukkan log e-wallet
                            Yii::app()->db->createCommand()->insert('sys_ewallet_log', array(
                                'ewallet_log_network_id' => Yii::app()->user->getState('network_id'),
                                'ewallet_log_type' => 'extra',
                                'ewallet_log_value' => $_POST['total_price'],
                                'ewallet_log_status' => 'debit',
                                'ewallet_log_datetime' => $datetime,
                                'ewallet_log_note' => 'Belanja Produk Belanja extra (Kode Transaksi: ' . $transaction_code . ')',
                            ));

                            $transaction->commit();

                            unset($_SESSION['extra']['cart']);
                            $_SESSION['extra'] = array();

                            Yii::app()->user->setFlash('success', 'Transaksi anda berhasil diproses');
                        } catch (CDbException $e) {
                            $transaction->rollback();
                            Yii::app()->user->setFlash('error', 'Transaksi anda gagal diproses. Silakan ulangi proses Transaksi.');
                        }
                        $this->redirect('list');
                    }
                }
            }
        }

        //data produk yang ada dalam keranjang belanja
        $cart = '';
        $total_quantity = $total_price = 0;
        if (isset($_SESSION['extra']['cart'])) {
            foreach ($_SESSION['extra']['cart'] as $products_id => $quantity) {
                $cart_products = vo_ecommerce::get_products_by_id($products_id);
                $cart[$products_id] = $cart_products;
                $cart[$products_id]['quantity'] = $quantity;
                $cart[$products_id]['total'] = $cart_products['products_price'] * $quantity;
                $total_quantity += $quantity;
                $total_price += $cart[$products_id]['total'];
            }
        }

        //data pengirim / member
        $member = sys_member::model()->findByPk(Yii::app()->user->getState('network_id'));

        $this->render('extra_checkout', array(
            'member' => $member,
            'cart' => $cart,
            'total_quantity' => $total_quantity,
            'total_price' => $total_price,
            'model' => $model,
        ));
    }

}
